package com.example.mypro;

import java.util.List;
import com.example.factory.ServiceFactory;
import com.example.service.InfoService;
import com.example.adapter.Dy_LvAdapter;
import com.example.domain.DianYing;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public  class DianyingActivity extends Activity implements OnClickListener,
		OnItemClickListener {

	/**
	 * @param args
	 */
	private ProgressBar pb;
	private InfoService infoService;
	private TextView tv;
	private GetData getData;
	private Button btn;
    private ListView listView;
	private List<DianYing> list = null;
	private Intent intent;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		infoService = ServiceFactory.getInfoService();
		setContentView(R.layout.activity_dianying);
		pb = (ProgressBar) findViewById(R.id.dy_pb);
		tv = (TextView) findViewById(R.id.dy_tv);
		btn = (Button) findViewById(R.id.dy_btn);
		listView = (ListView) findViewById(R.id.dy_lv);
		tv.setVisibility(View.INVISIBLE);
		btn.setVisibility(View.INVISIBLE);
		btn.setOnClickListener(this);
		listView.setVisibility(View.INVISIBLE);
		listView.setOnItemClickListener(this);
		pb.setVisibility(View.VISIBLE);
		getData = new GetData();
		getData.execute();

			}
	@SuppressLint("ShowToast")
	class GetData extends AsyncTask<Void, Void, List<DianYing>> {

		@SuppressLint("ShowToast")
		@Override
		protected List<DianYing> doInBackground(Void... params) {
			System.out.println("doInBackground");
			list = infoService.getDianYings();
			return list;
		}
		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);

		}

		@Override
		protected void onPostExecute(List<DianYing> result) {
			super.onPostExecute(result);
			pb.setVisibility(View.INVISIBLE);
			System.out.println("onProgressUpdate");
			if (list != null) {
				listView.setVisibility(View.VISIBLE);
				Dy_LvAdapter adapter = new Dy_LvAdapter(list,
						DianyingActivity.this);
				listView.setAdapter(adapter);
			} else {
				tv.setVisibility(View.VISIBLE);
				btn.setVisibility(View.VISIBLE);
			}
		}

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.dy_btn) {
			getData = new GetData();
			getData.execute();
		}

	}
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		DianYing dianYing = list.get(arg2);
		intent = new Intent(DianyingActivity.this, DianyingActivityItem.class);
		intent.putExtra("dy_tp", dianYing.getDy_tp());
		intent.putExtra("dy_bt", dianYing.getDy_bt());
		intent.putExtra("dy_jj", dianYing.getDy_jj());
		intent.putExtra("dy_dy", dianYing.getDy_dy());		
		intent.putExtra("dy_sj", dianYing.getDy_sj());
		intent.putExtra("dy_zy", dianYing.getDy_zy());
		intent.putExtra("dy_lx", dianYing.getDy_lx());
		intent.putExtra("dy_id", dianYing.getDy_id());
		startActivity(intent);
	}
		}

